package org.dsun.reflect;

import java.lang.reflect.Method;

public class Geter {
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Method fetchGeter(Class objectClass, String fieldName){
		
		StringBuilder mn=new StringBuilder();
		mn.append("get");
		mn.append(fieldName.substring(0, 1).toUpperCase());
		mn.append(fieldName.substring(1));
		
		try {
			return objectClass.getMethod(mn.toString());
		} catch (SecurityException e) {
//			e.printStackTrace();
			return null;
		} catch (NoSuchMethodException e) {
			mn.delete(0, 3);
			mn.insert(0, "is");
			try {
				Method m=objectClass.getMethod(mn.toString());
				if("boolean".equals(m.getReturnType().getName())){
					return m;
				}else{
					return null;
				}
			} catch (Exception e1) {
//				e.printStackTrace();
//				e1.printStackTrace();
				return null;
			}
		}
		
		
	}
	
	public static Object invokeGeter(Object o,String fieldName){
		Method m=fetchGeter(o.getClass(), fieldName);
		try {
			return m.invoke(o,new Object[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
